

module Schnell
  class Link < Element
    ELEMENT_TYPE = "a"
    
    # used internally to locate the link
    def locate
      if @how ==  :text
        @element = @page.findElementByLinkText(@what)
      elsif @how == :url or @how == :href
        @element = @page.findElementByXPath("//#{self.class::ELEMENT_TYPE}[@href='#{@what}']")
      else
        super
      end
    rescue
      @element = nil
    end
    
    private :locate
        
    # returns true if link has an image embedded in it
    def has_image?
      assert_exists
      return true if @element.getChildrenOfType("img").length > 0
      return false
    end    
    
    # return the src of the element
    def src
      assert_exists
      if @element.getChildrenOfType("img").length > 0
        return @element.getChildrenOfType("img")[0].getAttribute("src")
      else
        return ""
      end
    end
    
    # return the text of the element
    def innerText
      assert_exists
      return @element.getText
    end
    
    alias text innerText
    
    # return the href of the link
    def_wrap_guard :href
    # return the class name of the link
    def_wrap :class_name, :class
    # retun the type of the link
    def_wrap_guard :type
  end
end
